package ru.st.selenium.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class NewMoviePage extends AnyPage {

    public NewMoviePage(PageManager pages) {
        super(pages);
    }

    @FindBy(xpath = "//input[@name='imdbid']")
    private WebElement imdbNumberInput;

    @FindBy(xpath = "//input[@name='name']")
    private WebElement titleInput;

    @FindBy(xpath = "//textarea[@name='aka']")
    private WebElement alsoKnownAsInput;

    @FindBy(xpath = "//input[@name='year']")
    private WebElement yearInput;

    @FindBy(xpath = "//input[@name='duration']")
    private WebElement durationInput;

    @FindBy(xpath = "//input[@id='submit']")
    private WebElement submitBtn;

    @FindBy(xpath = "//input[@name='year']/ancestor::td/label[@for='year']")
    private WebElement yearAlert;

    public NewMoviePage setTitle(String title) {
        titleInput.clear();
        titleInput.sendKeys(title);
        return this;
    }

    public NewMoviePage setImdbNumber(String imbd) {
        imdbNumberInput.clear();
        imdbNumberInput.sendKeys(imbd);
        return this;
    }

    public NewMoviePage setAlsoKnown(String alsoKnown) {
        alsoKnownAsInput.clear();
        alsoKnownAsInput.sendKeys(alsoKnown);
        return this;
    }

    public NewMoviePage setYear(String year) {
        yearInput.clear();
        yearInput.sendKeys(year);
        return this;
    }

    public NewMoviePage setDuration(String duration) {
        durationInput.clear();
        durationInput.sendKeys(duration);
        return this;
    }

    public void clickSubmit() {
        submitBtn.click();
    }

    public String getYearAlertText() {
        return yearAlert.getText();
    }

    public NewMoviePage ensurePageLoaded() {
        super.ensurePageLoaded();
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='imdbid']")));
        return this;
    }

}
